package com.max.view;

import com.max.entity.Userinfo;
import com.max.service.UserinfoService;
import com.max.socket.client.ChatSocketClientThread;

import javax.swing.*;
import java.awt.*;

public class Login extends JFrame {


    public Login() {
        try {
            // 设置界面风格
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | UnsupportedLookAndFeelException | IllegalAccessException |
                 InstantiationException e) {
            throw new RuntimeException(e);
        }
//        JFrame frame = new JFrame("登录");
        setTitle("登录");
        setSize(400, 300);
        // 设置关闭模式
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        Font font = new Font("微软雅黑", Font.PLAIN, 14);

        JLabel accountLabel = new JLabel("账号：");
        JTextField accountTextField = new JTextField(15);
        JLabel passwordLabel = new JLabel("密码：");
        JPasswordField passwordField = new JPasswordField(15);
        JButton loginButton = new JButton("登 录");
        JButton registerButton = new JButton("注 册");
        accountLabel.setFont(font);
        accountTextField.setFont(font);
        passwordLabel.setFont(font);
        passwordField.setFont(font);
        loginButton.setFont(font);
        registerButton.setFont(font);

        registerButton.addActionListener(e -> new Register(this));

        //添加登录按钮的事件监听
        loginButton.addActionListener(e -> {
            String account = accountTextField.getText();
            String password = new String(passwordField.getPassword());
            try {
                Userinfo login = UserinfoService.getInstance().login(account, password);
                if (login != null) {
                    // 登录成功
                    JOptionPane.showMessageDialog(this, "登录成功");
                    // 跳转到聊天界面
                    // new ChatWindow();
                    new ChatSocketClientThread("127.0.0.1", 8080, login.getLoginName()).start();
                    dispose();
                } else {
                    // 登录失败
                    JOptionPane.showMessageDialog(this, "登录失败");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        });

        JPanel panel = new JPanel();
        // 设置面板布局方式为网格布局
        panel.setLayout(new GridBagLayout());
        // 创建布局对象，insets设置组件之间的间距,
        // gridx, gridy 设置组件的位置(左上角为原点，向右为X正方向)，向下为Y正方向
        // anchor 设置对齐方式
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(3, 4, 3, 4);
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(accountLabel, gbc);
        gbc.gridx = 1;
        panel.add(accountTextField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(passwordLabel, gbc);
        gbc.gridx = 1;
        panel.add(passwordField, gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(loginButton, gbc);
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(registerButton, gbc);

        //将内容面板添加到窗口
        setContentPane(panel);
        // 设置窗口的显示与隐藏
        setVisible(true);
    }

    public static void main(String[] args) {
        new Login();
        new Login();
        new Login();
    }


}
